package com.zzzzzz.core.dao;

public class BaseDao {
	
	protected String limit(){
		return " limit ?, ?";
    }
	
	protected String orderBy(String orderBy){
    	return " order by " + orderBy;
    }
	
	/*@Resource
	protected NamedParameterJdbcTemplate jt;
	
	protected <T> long updateForLongKey(final String sql, T t){
		SqlParameterSource param = new BeanPropertySqlParameterSource(t);
	    KeyHolder keyHolder = new GeneratedKeyHolder();  
	    jt.update(sql, param, keyHolder);
	    return keyHolder.getKey().longValue();
	}
	
	protected <T> int updateForIntKey(final String sql,T t){
		SqlParameterSource param = new BeanPropertySqlParameterSource(t);
	    KeyHolder keyHolder = new GeneratedKeyHolder();  
	    jt.update(sql, param, keyHolder);
	    return keyHolder.getKey().intValue();
	}
	
	protected <T> int update(final String sql, T t){
		SqlParameterSource param = new BeanPropertySqlParameterSource(t);
	    return jt.update(sql, param);
	}
	
	protected <T> void update(final String sql, Object... obj){
	    jt.getJdbcOperations().update(sql, obj);
	}
	
	protected <T> T findOne(boolean isEmptyNull, final String sql, RowMapper<T> rowMapper){
		if(isEmptyNull){
			List<T> tList = this.findList(sql, rowMapper);
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findOne(sql, rowMapper);
		}
	}
	protected <T> T findOne(final String sql, RowMapper<T> rowMapper){
		return jt.getJdbcOperations().queryForObject(sql, rowMapper);
	}
	
	protected <T> T findOne(boolean isEmptyNull, final String sql,RowMapper<T> rowMapper,Object... obj){
		if(isEmptyNull){
			List<T> tList = this.findList(sql, rowMapper, obj);
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findOne(sql, rowMapper, obj);
		}
	}
	protected <T> T findOne(final String sql,RowMapper<T> rowMapper,Object... obj){
		return jt.getJdbcOperations().queryForObject(sql, rowMapper, obj);
	}
	
	private class StringRowMapper implements RowMapper<String> {
		public String mapRow(ResultSet rs, int rn) throws SQLException {
			return rs.getString(1);
		}
	}
	private class LongRowMapper implements RowMapper<Long> {
		public Long mapRow(ResultSet rs, int rn) throws SQLException {
			return rs.getLong(1);
		}
	}
	private class IntRowMapper implements RowMapper<Integer> {
		public Integer mapRow(ResultSet rs, int rn) throws SQLException {
			return rs.getInt(1);
		}
	}
	
	protected String findString(boolean isEmptyNull, final String sql){
		if(isEmptyNull){
			List<String> tList = jt.getJdbcOperations().query(sql, new StringRowMapper());
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findString(sql);
		}
	}
	protected String findString(final String sql){
		return jt.getJdbcOperations().queryForObject(sql, String.class);
	}
	
	protected String findString(boolean isEmptyNull, final String sql, Object... obj){
		if(isEmptyNull){
			List<String> tList = jt.getJdbcOperations().query(sql, new StringRowMapper(), obj);
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findString(sql, obj);
		}
	}
	protected String findString(final String sql,Object... obj){
		return jt.getJdbcOperations().queryForObject(sql, String.class, obj);
	}

	protected Integer findInt(boolean isEmptyNull, final String sql){
		if(isEmptyNull){
			List<Integer> tList = jt.getJdbcOperations().query(sql, new IntRowMapper());
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findInt(sql);
		}
	}
	public int findInt(String sql){
		return jt.getJdbcOperations().queryForInt(sql);
	}
	
	protected Integer findInt(boolean isEmptyNull, final String sql, Object... obj){
		if(isEmptyNull){
			List<Integer> tList = jt.getJdbcOperations().query(sql, new IntRowMapper(), obj);
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findInt(sql, obj);
		}
	}
	public int findInt(String sql,Object... obj){
		return jt.getJdbcOperations().queryForInt(sql,obj);
	}
	
	protected Long findLong(boolean isEmptyNull, final String sql){
		if(isEmptyNull){
			List<Long> tList = jt.getJdbcOperations().query(sql, new LongRowMapper());
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findLong(sql);
		}
	}
	public long findLong(String sql){
		return jt.getJdbcOperations().queryForLong(sql);
	}
	
	protected Long findLong(boolean isEmptyNull, final String sql, Object... obj){
		if(isEmptyNull){
			List<Long> tList = jt.getJdbcOperations().query(sql, new LongRowMapper(), obj);
			if(tList.isEmpty()){
				return null;
			}else if(tList.size() == 1){ // list contains exactly 1 element
				return tList.get(0);
			}else{
				throw new IncorrectResultSizeDataAccessException(tList.size());
			}
		}else{
			return this.findLong(sql, obj);
		}
	}
	public long findLong(String sql,Object... obj){
		return jt.getJdbcOperations().queryForLong(sql,obj);
	}
	
	public <T> List<T> findList(final String sql, RowMapper<T> mapper) {
		List<T> results = jt.getJdbcOperations().query(sql, mapper);
		return results;
	}
	 
	public <T> List<T> findList(final String sql, RowMapper<T> rowMapper, Object... obj) {
		List<T> results = null;
		results = jt.getJdbcOperations().query(sql, rowMapper, obj);
		return results;
	}
	
	public <T> List<T> findList(final String sql, MapSqlParameterSource paramMap, RowMapper<T> rowMapper) {
		List<T> results = jt.query(sql, paramMap, rowMapper);
		return results;
	}
	
	
	protected String limit(MapSqlParameterSource paramMap, int start, int offset){
    	paramMap.addValue("start", start);
    	paramMap.addValue("offset", offset);
    	return " limit :start,:offset";
    }
	
	public SystemException processException(Exception e) {
		if (!(e instanceof DataAccessException)) {
			logger.error("Caught unexpected exception " + e.getClass().getName());
		}

		if (logger.isDebugEnabled()) {
			logger.debug(e.getMessage(), e);
		}

		return new SystemException(e);
	}

	private static final Logger logger = LoggerFactory.getLogger(BaseDao.class);*/
	
}
